#include <qapplication.h>
#include <qevent.h>
#include <qpainter.h>
#include <qwt_plot.h>
#include <qwt_symbol.h>
#include <qwt_scale_map.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>

#include "canvaspicker.h"
#include "globalconstants.h"

CanvasPicker::CanvasPicker(QwtPlot *plot):
    QObject(plot),
    d_selectedCurve(NULL),
    d_selectedPoint(-1)
{
    QwtPlotCanvas *canvas = plot->canvas();

    canvas->installEventFilter(this);
    

    connect(this, SIGNAL(selectTank(int,int)),plot, SLOT(selectTank(int,int)));
    connect(plot, SIGNAL(newTankId(QwtPlotCurve*,int)),this, SLOT(newTankId(QwtPlotCurve*,int)));
}

bool CanvasPicker::event(QEvent *e)
{
    if ( e->type() == QEvent::User )
    {
        showCursor(true);
        return true;
    }
    return QObject::event(e);
}

bool CanvasPicker::eventFilter(QObject *object, QEvent *e)
{
    if ( object != (QObject *)plot()->canvas() )
        return false;

    switch(e->type())
    {
        case QEvent::FocusIn:
            showCursor(true);
        case QEvent::FocusOut:
            showCursor(false);

        case QEvent::Paint:
        {   
            QApplication::postEvent(this, new QEvent(QEvent::User));
            break;
        }
        case QEvent::MouseButtonPress:
        {
            select(((QMouseEvent *)e)->pos());
            return true; 
        }
        default:
            break;
    }
    return QObject::eventFilter(object, e);
}

// Select the point at a position. If there is no point
// deselect the selected point

void CanvasPicker::select(const QPoint &pos)
{
    QwtPlotCurve *curve = NULL;
    double dist = 10e10;
    int index = -1;

    const QwtPlotItemList& itmList = plot()->itemList();
    for ( QwtPlotItemIterator it = itmList.begin(); it != itmList.end(); ++it )
    {
        if ( (*it)->rtti() == QwtPlotItem::Rtti_PlotCurve )
        {
            QwtPlotCurve *c = (QwtPlotCurve*)(*it);

            double d;
            int idx = c->closestPoint(pos, &d);
            if ( d < dist )
            {
                curve = c;
                index = idx;
                dist = d;
                if ( curve && dist < 10 ) // 10 pixels tolerance
                {

                  int curveId=-1;
                  if(curve->symbol().brush()==Qt::white){
                    curveId=0;
                    return; //Esto lo hago para impedir la seleccion de estaciones cero
                  } 
                  if(curve->symbol().brush()==colorToT) curveId=1;
                  if(curve->symbol().brush()==colorThr2) curveId=2;
                  if(curve->symbol().brush()==colorThr1) curveId=3;
                  
                  d_selectedCurve = curve;
                  d_selectedPoint = index;
                  showCursor(true);
                  emit selectTank(curveId,index);
                  plot()->replot();
                }
            }
        }
    }
}

void CanvasPicker::newTankId(QwtPlotCurve* curve,int index)
{
  d_selectedCurve = curve;
  d_selectedPoint = index;
  showCursor(true);
  plot()->replot();
}

// Hightlight the selected point
void CanvasPicker::showCursor(bool showIt)
{
    if ( !d_selectedCurve )
        return;

    const QwtSymbol symbol = d_selectedCurve->symbol();

    QwtSymbol newSymbol(QwtSymbol::Ellipse,Qt::white, Qt::NoPen, QSize(15, 15));// = symbol;
    if ( showIt )
        newSymbol.setBrush(symbol.brush().color().dark(90));

    const bool doReplot = plot()->autoReplot();

    plot()->setAutoReplot(false);
    d_selectedCurve->setSymbol(newSymbol);

    d_selectedCurve->draw(d_selectedPoint, d_selectedPoint);

    d_selectedCurve->setSymbol(symbol);
    plot()->setAutoReplot(doReplot);
}



